package cn.e2win.biz.user;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysFunction;
import cn.e2win.base.entity.SysMenu;
import cn.e2win.base.entity.SysRole;
import cn.e2win.base.entity.SysRoleDao;
import cn.e2win.base.entity.SysRoleFunction;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAO;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.menu.MenuService;
import cn.e2win.biz.security.UserSession;
import cn.e2win.biz.util.StringUtils;


@Service
public class RoleService extends BaseService{
	
	
	@Autowired
	private SysRoleDao sysRoleDao;
	@Autowired
	private MenuService menuService;
	
	@Override
	public BaseDAO initDao() {
		return sysRoleDao;
	}

	public void pagerRole(Pager pager){
		sysRoleDao.pagerRole(pager);
	}
	
	
	

	@Transactional(rollbackFor=Exception.class)
	public SysRole editRole(SysRole role,String[] funIds) throws MyException {
		
		role = (SysRole)super.edit(role);
		updateRoleFunction(role, funIds);
		
		return role;
	}
	
	private void updateRoleFunction(SysRole role, String[] funIds) throws MyException {
		//清空原来选择的
		sysRoleDao.delRoleFunctionByRoleId(role.getId());
		
		List<SysRoleFunction> roleFunList = new ArrayList<SysRoleFunction>();
		//设置新选择的
		List<SysFunction> funList = sysRoleDao.getFunctionListByIds(StringUtils.join(funIds, ","));
		for(SysFunction fun:funList){
			SysRoleFunction roleFun = new SysRoleFunction();
			roleFun.setFunId(fun.getId());
			roleFun.setRoleId(role.getId());
			roleFun.setCreateById(UserSession.getUser().getId());
			roleFun.setCreateDate(role.getCreateDate());
			roleFunList.add(roleFun);
			
		}
		sysRoleDao.saveAll(roleFunList);
	}
	
	
	
	
	
	
	
	public void getMenuTree(Integer roleId, ModelMap modelMap){
		SysRole role = (SysRole)getEntity(SysRole.class,roleId);
		List<SysRoleFunction> roleFuncList = sysRoleDao.getRoleFunctionListByRoleId(roleId);		
		modelMap.put("object", role);
		
		List<SysMenu> allMenus = menuService.getAll();
		for(SysMenu menu : allMenus){
			List<SysFunction> menuFuncList = menu.getFunctionList();
			for (SysFunction oneOfAll : menuFuncList) {
				for(SysRoleFunction  oneOfRole : roleFuncList){
					if (oneOfAll.getId().equals(oneOfRole.getFunId())) {
						//暂时用这个字段代替,不会被保存到数据库,只提供给页面来选中
						oneOfAll.setEnable(1);
					}
				}
			}
		}
		modelMap.put("allMenus", allMenus);
	}
	
	
	
	
	
	/*
	
	public void assignFuntions(Integer id, String[] ids){
		//TODO 需修改重程序，太复杂。
		SysRole role = (SysRole)getEntity(SysRole.class,id);
		Set<SysFunction> roleFunctions = role.getAppliedFunctions();
		//已经删除的(在角色管理中的逻辑删除)也一并删除
		for(Iterator<SysFunction> iterator = roleFunctions.iterator(); iterator.hasNext();){
			SysFunction function = iterator.next();
			if (function.getActiveStatusInt() != null && function.getActiveStatusInt().intValue() == 0) {
				iterator.remove();
			}
		}
		List<SysRoleFunction> funList = sysRoleDao.getRoleFunctionListByRoleId(id);
		//去除已经删除的function
		for(int i=0;i<funList.size();i++){
			
			if (funList.get(i).getActiveStatusInt() != null && funList.get(i).getActiveStatusInt().intValue() == 0) {
			//	funList.set(i, null);
			}
		}
		
		//在页面中被取消的
		for(int i=0;i<funList.size();i++){
			SysRoleFunction function = funList.get(i);
			if(function != null){
				Integer functionId = function.getId();
				boolean found = false;
				for(String pageFunctionId : ids){
					if((functionId+"").equals(pageFunctionId)){
						found = true;
						break;
					}
				}
				if(!found){
					funList.set(i, null);
				}
			}
		}
		
		//在页面中新增的
		for(String pageFunctionId : ids){
			boolean found = false;
			for(SysRoleFunction oneOfRoleFunction : funList){
				if(oneOfRoleFunction != null && pageFunctionId.equals(oneOfRoleFunction.getId()+"")){
					found = true;
					break;
				}
			}
			if(!found && StringUtils.isNumeric(pageFunctionId)){
				SysRoleFunction newFunction = new SysRoleFunction();
				newFunction.setFunId(new Integer(pageFunctionId));
				newFunction.setRoleId(id);
				
				funList.add(newFunction);
			}
		}
		String funDesc = "";
		for(SysRoleFunction temp : funList){
			if(temp!=null){
				//funDesc = funDesc + temp.getTitle() + "&nbsp;";
				sysRoleDao.merge(temp);
			}
		}
		role.setFunDesc(funDesc);
		
		
		
		
		sysRoleDao.save(role);
	}
*/
	public List<SysRole> getAllRole() {
		return sysRoleDao.getAllRole();
	}


	
}
